#pragma once
#include "kernel_operator.h"
#include "op_common.h"
namespace AscendC {
template <typename T> struct LocalList {
  int64_t size_ = 0;
  DefBufVECIN(DATA);
  LocalTensor<T> data_;
  int64_t capicty_ = 0;

  __aicore__ inline LocalList() : size_(0) {}
  __aicore__ inline void init(TPipe &pipe, int64_t capicity) {
    size_ = 0;
    InitTBufBuffer(DATA, capicity);
    TBufGet(DATA, T);
    data_ = BTensor(DATA);
    capicty_ = capicity / sizeof(T);
  }
  __aicore__ inline void push_front(T v) {
    index_move_right(0);
    data_.SetValue(0, v);
  }
  __aicore__ inline T pop_front() {
    auto ret = data_(0);
    index_move_left(0);
    return ret;
  }
  __aicore__ inline void push_back(T v) { data_.SetValue(size_++, v); }
  __aicore__ inline T pop_back() { return data_(--size_); }
  __aicore__ inline bool empty() { return size_ == 0; }
  __aicore__ inline void insert(int64_t index, T v) {
    index_move_right(index);
    data_.SetValue(index, v);
  }
  __aicore__ inline void remove(int64_t index) { index_move_left(index); }
  __aicore__ inline void clear() { size_ = 0; }

private:
  __aicore__ inline void index_move_left(int64_t index) {
    for (auto i = index; i < size_ - 1; ++i) {
      data_.SetValue(i, data_.GetValue(i + 1));
    }
    size_--;
  }
  __aicore__ inline void index_move_right(int64_t index) {
    for (auto i = index + 1; i <= size_; ++i) {
      data_.SetValue(i, data_.GetValue(i - 1));
    }
    size_++;
  }
};
} // namespace AscendC